<template>
  <el-dialog :title="type === 'add' ? '新增收藏' : '编辑收藏'"
    :visible="dialogVisible"
    width="650px"
    @close="close">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-row>
        <el-col :span="12">
          <el-form-item label="收藏名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收藏网址" prop="url">
            <el-input v-model="ruleForm.url"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库地址" prop="repository">
            <el-input v-model="ruleForm.repository"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收藏类别" prop="tid">
            <el-select
              v-model="ruleForm.tid"
              filterable
              allow-create
              default-first-option
              placeholder="请选择">
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="网址logo" prop="logo">
            <el-input v-model="ruleForm.logo">
              <template slot="prepend">https://badgen.net/badge/color/</template>
              <template slot="append">/cyan?label</template>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="收藏标签" prop="inputValue">
            <d-tag :dynamicTags.sync="dynamicTags" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="ruleForm.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset('ruleForm')">取 消</el-button>
      <el-button v-if="type === 'add'" type="primary" @click="save('ruleForm')" :loading="loading">确 定</el-button>
      <el-button v-if="type === 'edit'" type="primary" @click="update('ruleForm')" :loading="loading">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      ruleForm: {
        name: '',
        url: '',
        logo: '',
        repository: '',
        tid: '',
        remark: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入收藏名称', trigger: 'blur' }
        ],
        url: [
          { required: true, message: '请输入收藏网址', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '请输入网址logo', trigger: 'blur' }
        ],
      },
      loading: false,
      type: 'add',
      typeOptions: [],
      dynamicTags: []
    }
  },
  props: {
    dialogVisible: {
      type: Boolean
    },
    list: {
      type: Array
    }
  },
  created () {
  },
  methods: {
    close () {
      this.$refs.ruleForm.resetFields()
      this.dynamicTags = []
      this.$emit('update:dialogVisible', false)
    },
    open () {
      this.type = 'add'
      this.getTypeList()
      this.$emit('update:dialogVisible', true)
    },
    save(formName) { // 新增
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            name: this.ruleForm.name,
            url: this.ruleForm.url,
            logo: `https://badgen.net/badge/color/${this.ruleForm.logo}/cyan?label`,
            repository: this.ruleForm.repository,
            tag: this.dynamicTags.join(','),
            uid: this.$Cookies.get('uid'),
            tid: this.ruleForm.tid,
            remark: this.ruleForm.remark
          }
          this.$API({
            name: 'addCollect',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    reset(formName) {
      this.$refs[formName].resetFields()
      this.dynamicTags = []
      this.$emit('update:dialogVisible', false)
    },
    detaliInfo (id) { // 根据id获取详情
      this.type = 'edit'
      this.getTypeList()
      this.$emit('update:dialogVisible', true)
      this.$API({
        name: 'detailCollect',
        paths: [id],
        requireAuth: true
      }).then (res => {
        if (res.data.code === 200) {
          this.ruleForm = res.data.data
          this.ruleForm.logo = this.ruleForm.logo.split('color/')[1].split('/cyan')[0]
          this.dynamicTags = this.ruleForm.tag.split(',')
        } else {
          this.$message.error(res.data.msg)
        }
      }).catch(error => {
        this.$message.error(error)
      })
    },
    update(formName) { // 编辑
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true
          const params = {
            id: this.ruleForm.id,
            name: this.ruleForm.name,
            url: this.ruleForm.url,
            logo: `https://badgen.net/badge/color/${this.ruleForm.logo}/cyan?label`,
            repository: this.ruleForm.repository,
            tag: this.dynamicTags.join(','),
            uid: this.ruleForm.uid,
            tid: this.ruleForm.tid,
            remark: this.ruleForm.remark
          }
          this.$API({
            name: 'updateCollect',
            data: params,
            requireAuth: true
          }).then(res => {
            if (res.data.code === 200 && res.data.data[0] === 1) {
              this.$message.success(res.data.msg)
              this.close()
              this.$emit('getAll')
            } else {
              this.$message.error(res.data.msg)
              this.close()
            }
          }).catch(error => {
            this.$message.error(error)
          }).finally(() => {
            this.loading = false
          })
        }
      })
    },
    async getTypeList () {
      const res = await this.$API({
        name: 'getCollectTypesByUid',
        paths: [this.$Cookies.get('uid')],
        requireAuth: true
      })
      let arr = []
      res.data.data.forEach(item => {
        arr.push({
          id: item.id,
          name: item.name
        })
      })
      this.typeOptions = arr
    }
  }
}
</script>

<style>
</style>